package com.fr.web;

import com.fr.base.FCloneable;
import com.fr.base.FRContext;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.TableData;
import com.fr.report.PageSetChain;
import com.fr.report.Report;
import com.fr.report.ReportParameterAttr;
import com.fr.report.ReportWebAttr;
import com.fr.report.ResultWorkBook;
import com.fr.report.TemplateReport;
import com.fr.report.TemplateWorkBook;
import com.fr.report.io.ReportExportAttr;
import com.fr.report.parameter.Parameter;
import com.fr.web.core.ShowWorkBookPolicy;
import com.fr.web.core.WebUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MultiTemplateReportlet extends Reportlet
{
  private static final Pattern KEY_VALUE_PATTERN = Pattern.compile("[^{,}]*:[^{,}]*");
  private String reportletsinfo = null;
  private ShowWorkBookPolicy policy = null;
  private Map parameter4Execute = null;

  public MultiTemplateReportlet(String paramString, ShowWorkBookPolicy paramShowWorkBookPolicy, Map paramMap)
  {
    this.reportletsinfo = paramString;
    setPolicy(paramShowWorkBookPolicy);
    this.parameter4Execute = paramMap;
  }

  public TemplateWorkBook createReport(ReportletRequest paramReportletRequest)
    throws ReportletException
  {
    ArrayList localArrayList1 = new ArrayList();
    ArrayList localArrayList2 = new ArrayList();
    ArrayList localArrayList3 = new ArrayList();
    createReportsFromReportlets(this.reportletsinfo, localArrayList3, localArrayList1, localArrayList2, paramReportletRequest);
    Object localObject1 = null;
    Object localObject2 = null;
    TemplateWorkBook localTemplateWorkBook = null;
    ResultWorkBook localResultWorkBook = null;
    for (int i = 0; (i < localArrayList1.size()) && (localArrayList1.size() > i); ++i)
    {
      int j;
      localTemplateWorkBook = (TemplateWorkBook)localArrayList1.get(i);
      localResultWorkBook = getPolicy().show(localTemplateWorkBook, getPatameterMap4Execute((localArrayList2.size() > i) ? (Map)localArrayList2.get(i) : null));
      if (localObject1 == null)
        localObject1 = localTemplateWorkBook;
      else
        for (j = 0; j < localTemplateWorkBook.getReportCount(); ++j)
          localObject1.addReport(localTemplateWorkBook.getReportName(j), localTemplateWorkBook.getTemplateReport(j));
      if (localObject2 == null)
        localObject2 = localResultWorkBook;
      else
        for (j = 0; j < localResultWorkBook.getReportCount(); ++j)
          localObject2.addReport(localResultWorkBook.getReportName(j), localResultWorkBook.getResultReport(j));
    }
    return new MultiTempalteWorkBook(localObject1, localObject2, null);
  }

  public void setPolicy(ShowWorkBookPolicy paramShowWorkBookPolicy)
  {
    this.policy = paramShowWorkBookPolicy;
  }

  public ShowWorkBookPolicy getPolicy()
  {
    return this.policy;
  }

  private Map getPatameterMap4Execute(Map paramMap)
  {
    HashMap localHashMap = new HashMap();
    if (this.parameter4Execute != null)
      localHashMap.putAll(this.parameter4Execute);
    if (paramMap != null)
      localHashMap.putAll(paramMap);
    for (int i = 0; i < ParameterConsts.ALL.length; ++i)
      if (localHashMap.containsKey(ParameterConsts.ALL[i]))
        localHashMap.remove(ParameterConsts.ALL[i]);
    return localHashMap;
  }

  private void createReportsFromReportlets(String paramString, List paramList1, List paramList2, List paramList3, ReportletRequest paramReportletRequest)
  {
    JSONArray localJSONArray;
    int i;
    paramList1.clear();
    paramList2.clear();
    paramList3.clear();
    if (paramString == null)
      return;
    paramString = paramString.trim();
    String str1 = null;
    if (paramString.startsWith("("))
      str1 = transferToJSONString(paramString);
    else
      str1 = paramString;
    FRContext.getLogger().log(Level.INFO, "reportletsInfo:" + str1);
    if (str1 == null)
      return;
    try
    {
      localJSONArray = new JSONArray(str1);
      for (i = 0; i < localJSONArray.length(); ++i)
      {
        Object localObject1;
        HashMap localHashMap = new HashMap();
        JSONObject localJSONObject = localJSONArray.getJSONObject(i);
        Iterator localIterator = localJSONObject.keys();
        while (localIterator.hasNext())
        {
          localObject1 = (String)localIterator.next();
          Object localObject2 = localJSONObject.get((String)localObject1);
          localHashMap.put(localObject1, localObject2);
        }
        String str2 = (String)localHashMap.get("reportlet");
        if (str2 == null)
          break label263:
        try
        {
          localObject1 = WebUtils.reportlet2ReportTemplate(str2, paramReportletRequest);
          paramList1.add(str2);
          paramList2.add(localObject1);
          label263: paramList3.add(localHashMap);
        }
        catch (Exception localException)
        {
          FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
        }
      }
    }
    catch (JSONException localJSONException)
    {
      FRContext.getLogger().log(Level.WARNING, localJSONException.getMessage(), localJSONException);
    }
  }

  public static String transferToJSONString(String paramString)
  {
    if (paramString == null)
      return null;
    paramString = paramString.trim();
    if (paramString.length() <= 0)
      return null;
    paramString = paramString.substring(1, paramString.length() - 1);
    StringBuffer localStringBuffer = new StringBuffer("[");
    Matcher localMatcher = KEY_VALUE_PATTERN.matcher(paramString);
    int i = 0;
    int j = 0;
    while (true)
    {
      String[] arrayOfString;
      while (true)
      {
        if (!(localMatcher.find()))
          break label161;
        j = localMatcher.start();
        String str1 = paramString.substring(i, j);
        if ((str1 != null) && (str1.length() > 0))
          localStringBuffer.append(str1);
        i = localMatcher.end();
        String str2 = localMatcher.group();
        arrayOfString = str2.split(":");
        if (arrayOfString.length == 2)
          break;
        localStringBuffer.append(str2);
      }
      localStringBuffer.append(arrayOfString[0]).append(":").append(JSONObject.quote(arrayOfString[1]));
    }
    label161: localStringBuffer.append(paramString.substring(i, paramString.length()));
    localStringBuffer.append("]");
    return localStringBuffer.toString();
  }

  private static class MultiTempalteWorkBook
    implements TemplateWorkBook
  {
    private TemplateWorkBook template;
    private ResultWorkBook result;

    private MultiTempalteWorkBook(TemplateWorkBook paramTemplateWorkBook, ResultWorkBook paramResultWorkBook)
    {
      this.template = paramTemplateWorkBook;
      this.result = paramResultWorkBook;
    }

    public void addReport(TemplateReport paramTemplateReport)
    {
      throw new UnsupportedOperationException();
    }

    public void addReport(String paramString, TemplateReport paramTemplateReport)
    {
      throw new UnsupportedOperationException();
    }

    public void apply4Parameters(Map paramMap)
    {
      this.template.apply4Parameters(paramMap);
    }

    public ResultWorkBook execute(Map paramMap)
    {
      return this.result;
    }

    public ResultWorkBook execute4Form(Map paramMap)
    {
      return this.result;
    }

    public ResultWorkBook execute4Write(Map paramMap)
    {
      return this.result;
    }

    public Parameter[] getParameters()
    {
      return getParameters();
    }

    public int getReportIndex(TemplateReport paramTemplateReport)
    {
      return this.template.getReportIndex(paramTemplateReport);
    }

    public ReportParameterAttr getReportParameterAttr()
    {
      return this.template.getReportParameterAttr();
    }

    public TemplateReport getTemplateReport(int paramInt)
    {
      return this.template.getTemplateReport(paramInt);
    }

    public void setReport(int paramInt, TemplateReport paramTemplateReport)
    {
      throw new UnsupportedOperationException();
    }

    public void setReport(int paramInt, String paramString, TemplateReport paramTemplateReport)
    {
      throw new UnsupportedOperationException();
    }

    public void setReportParameterAttr(ReportParameterAttr paramReportParameterAttr)
    {
      this.template.setReportParameterAttr(paramReportParameterAttr);
    }

    public PageSetChain generateReportPageSet(List paramList)
    {
      return this.template.generateReportPageSet(paramList);
    }

    public Report getReport(int paramInt)
    {
      return this.template.getReport(paramInt);
    }

    public int getReportCount()
    {
      return this.template.getReportCount();
    }

    public ReportExportAttr getReportExportAttr()
    {
      return this.template.getReportExportAttr();
    }

    public String getReportName(int paramInt)
    {
      return this.template.getReportName(paramInt);
    }

    public ReportWebAttr getReportWebAttr()
    {
      return this.template.getReportWebAttr();
    }

    public void removeReport(String paramString)
    {
      throw new UnsupportedOperationException();
    }

    public void removeReport(int paramInt)
    {
      throw new UnsupportedOperationException();
    }

    public void removeReports()
    {
      throw new UnsupportedOperationException();
    }

    public void setReportExportAttr(ReportExportAttr paramReportExportAttr)
    {
      this.template.setReportExportAttr(paramReportExportAttr);
      this.result.setReportExportAttr(paramReportExportAttr);
    }

    public void setReportName(int paramInt, String paramString)
    {
      this.template.setReportName(paramInt, paramString);
      this.result.setReportName(paramInt, paramString);
    }

    public void setReportWebAttr(ReportWebAttr paramReportWebAttr)
    {
      this.template.setReportWebAttr(paramReportWebAttr);
      this.result.setReportWebAttr(paramReportWebAttr);
    }

    public void readXML(XMLableReader paramXMLableReader)
    {
      throw new UnsupportedOperationException();
    }

    public void writeXML(XMLPrintWriter paramXMLPrintWriter)
    {
      throw new UnsupportedOperationException();
    }

    public void clearAllTableData()
    {
      this.template.clearAllTableData();
    }

    public TableData getTableData(String paramString)
    {
      return this.template.getTableData(paramString);
    }

    public Iterator getTableDataNameIterator()
    {
      return this.template.getTableDataNameIterator();
    }

    public void putTableData(String paramString, TableData paramTableData)
    {
      throw new UnsupportedOperationException();
    }

    public void removeTableData(String paramString)
    {
      throw new UnsupportedOperationException();
    }

    public boolean renameTableData(String paramString1, String paramString2)
    {
      throw new UnsupportedOperationException();
    }

    public Object clone()
      throws CloneNotSupportedException
    {
      MultiTempalteWorkBook localMultiTempalteWorkBook = (MultiTempalteWorkBook)super.clone();
      localMultiTempalteWorkBook.template = ((TemplateWorkBook)this.template.clone());
      localMultiTempalteWorkBook.result = ((ResultWorkBook)this.result.clone());
      return localMultiTempalteWorkBook;
    }

    MultiTempalteWorkBook(TemplateWorkBook paramTemplateWorkBook, ResultWorkBook paramResultWorkBook, MultiTemplateReportlet.1 param1)
    {
      this(paramTemplateWorkBook, paramResultWorkBook);
    }
  }
}